package com.ttc.group2.videochat.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.quickblox.videochat.core.QBVideoChatController;
import com.quickblox.videochat.model.objects.CallType;
import com.quickblox.videochat.model.objects.VideoChatConfig;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.VideoChatApplication;
import com.ttc.group2.videochat.view.OpponentSurfaceView;
import com.ttc.group2.videochat.view.OwnSurfaceView;
import com.ttc.group2.videochat.view.OwnSurfaceView.CameraDataListener;

public class VideoCallActivity extends Activity {
	private ImageButton btnStop;
	private ImageButton btnSwitch;
	private ToggleButton btnMute;
	private OwnSurfaceView ownView;
	private OpponentSurfaceView opponentView;
	private VideoChatConfig videoChatConfig;
	private TextView txtName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_call);

		// init view
		initView();

		// init Listener
		initListener();
	}

	@Override
	protected void onResume() {
		super.onResume();
		ownView.reuseCamera();
	}

	@Override
	protected void onPause() {
		super.onPause();
		ownView.closeCamera();
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (videoChatConfig != null) {
			QBVideoChatController.getInstance()
					.finishVideoChat(videoChatConfig);
		}
	}

	private void initView() {
		ownView = (OwnSurfaceView) findViewById(R.id.ownVideoScreenImageView);
		opponentView = (OpponentSurfaceView) findViewById(R.id.opponentImageView);
		btnStop = (ImageButton) findViewById(R.id.stopСallButton);
		btnSwitch = (ImageButton) findViewById(R.id.switch_button);
		btnMute = (ToggleButton) findViewById(R.id.muteMicrophoneButton);
		txtName = (TextView) findViewById(R.id.txtOpponentName);
	}

	private void initListener() {
		final VideoChatApplication app = (VideoChatApplication) getApplication();

		// VideoChat settings
		videoChatConfig = getIntent().getParcelableExtra(
				VideoChatConfig.class.getCanonicalName());

		txtName.setText(app.getCurrentUser().getFullName());

		//
		btnSwitch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ownView.switchCamera();
			}
		});

		//
		ownView.setCameraDataListener(new CameraDataListener() {

			@Override
			public void onCameraDataReceive(byte[] data) {
				if (videoChatConfig != null
						&& videoChatConfig.getCallType() != CallType.VIDEO_AUDIO) {
					return;
				}
				QBVideoChatController.getInstance().sendVideo(data);
			}
		});

		btnStop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				QBVideoChatController.getInstance().stopCalling();
				QBVideoChatController.getInstance().onDestroy();
				opponentView.clear();

			}
		});

	}

}
